﻿using System;
using System.IO;



namespace Controller_Interface
{
    class NextPositionWatcher
    {
        private FileSystemWatcher _watcher;
        private readonly String _inputFilePathAndName;
        private readonly String _inputTemp;

        private String _line1Old;
        private String _line2Old;

        private bool _flag;

        public NextPositionWatcher(string inputFilePathAndName)
        {
            _inputFilePathAndName = inputFilePathAndName;
            _inputTemp = Path.GetDirectoryName(_inputFilePathAndName)+"\\345423432fe43.tmp";

            _line1Old = String.Empty;
            _line2Old = String.Empty;

            InitWatcher();
        }

        private void InitWatcher()
        {

            _watcher = new FileSystemWatcher
            {
                Path = Path.GetDirectoryName(_inputFilePathAndName),
                Filter = Path.GetFileName(_inputFilePathAndName),
                NotifyFilter = NotifyFilters.Attributes
            };

            _watcher.Changed += FileChanged;
            _watcher.EnableRaisingEvents = true;
        }

        #region Events

        private void FileChanged(object sender, FileSystemEventArgs e)
        {

            // doua evenimente sunt declansate la modificarea unui fisier
            // numai unul se trateaza
            if (!_flag)
            {
                _flag = true;
                return;
            }

            _flag = false;


            String[] values = ReadFromFile();

            if (values != null && !(values[0].Equals(_line1Old) && values[1].Equals(_line2Old)))
            {
                OnRobotPositionChanged(new RobotPositionChangedEventArgs(values[0], values[1]));
                _line1Old = values[0];
                _line2Old = values[1];
            }
        }
        #endregion

        private String[] ReadFromFile()
        {
            var values = new[] {String.Empty, String.Empty, String.Empty};

            var fileInfo = new FileInfo(_inputFilePathAndName);

            if (!fileInfo.Exists)
                return null;

            //it makes a copy 
            (new FileInfo(_inputFilePathAndName)).CopyTo(_inputTemp, true);


            //it  reads information
            using (StreamReader reader = File.OpenText(_inputTemp))
            {

                short index = 0;
                String input;

                while ((input = reader.ReadLine()) != null)
                {
                    values[index] = input;
                    index++;
                    if (index > 1)
                        continue;
                }
                reader.Close();
            }

            return values;
        }

        #region secret

        public event EventHandler<RobotPositionChangedEventArgs> RobotPositionChanged;

        protected virtual void OnRobotPositionChanged(RobotPositionChangedEventArgs e)
        {
            if (RobotPositionChanged != null) RobotPositionChanged(this, e);
        }

        #endregion
    }

    public class RobotPositionChangedEventArgs : EventArgs
    {
        public readonly String Position;
        public readonly String Orientation;

        public RobotPositionChangedEventArgs(string position, string orientation)
        {
            Position = position;
            Orientation = orientation;
        }
    }

}
